package com.fancy.learncenter.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.adapter.DataAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.DataCategoryDataBean;
import com.fancy.learncenter.bean.TopNewsDataBean;
import com.fancy.learncenter.bean.TzDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PopupWindowMaterrial;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class DataFragment extends Fragment {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.rootView)
    RelativeLayout rootView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.type_image)
    ImageView typeImage;
    @Bind(R.id.prompt_img)
    ImageView promptImg;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_data, container, false);
        ButterKnife.bind(this, view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        newsAdapter = new DataAdapter(getActivity(), new ArrayList<TopNewsDataBean>());
        recycleView.setAdapter(newsAdapter);

        newsAdapter.setCustomLoadMoreView(new CustomFootView(getActivity()));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                categoryId = "";
                getAccount(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getAccount(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getAccount(true);

        popupWindowMaterrial = new PopupWindowMaterrial(getActivity(), rootView);
        popupWindowMaterrial.setOnCallBack(new PopupWindowMaterrial.OnCallBack() {
            @Override
            public void onCallBack(DataCategoryDataBean.ListBean items) {
                categoryId = items.getId();
                getAccount(true);
            }

        });

        return view;


    }

    String categoryId = "";

    PopupWindowMaterrial popupWindowMaterrial;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    DataAdapter newsAdapter;

    int page = 0;

    private void getAccount(final boolean isRefresh) {
        Map maps = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        maps.put("page", page + "");


        maps.put("categoryId", categoryId);
        HttpMehtod.getInstance().dataLists(maps, new IdeaObserver<BaseDataBean<List<TopNewsDataBean>>>(getActivity(), true) {

            @Override
            public void onSuccess(BaseDataBean<List<TopNewsDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    newsAdapter.notifyDataSetChanged((ArrayList<TopNewsDataBean>) listBaseDataBean.getData());
                } else {
                    newsAdapter.notifyDataSetChanged(new ArrayList<TopNewsDataBean>());
                }

                if (isRefresh) {

                    if (xRefreshView == null) {
                        return;
                    }
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        promptImg.setVisibility(View.VISIBLE);
                        newsAdapter.notifyDataSetChanged(new ArrayList());
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        newsAdapter.notifyDataSetChanged((ArrayList<TopNewsDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    newsAdapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }

        });
    }

    @OnClick(R.id.type_image)
    public void onViewClicked() {
        popupWindowMaterrial.showPopupWindow();
    }
}
